<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>单多个文件上传</title>
    </head>
    <body>
        <!-- 方法必须为POST -->
    <form class="" action="demo.php" method="post" enctype="multipart/form-data">
        Username: <input type="text" name="username" value=""><br>
        <!-- 多个文件上传用到三维数组来定义 name 属性的值，在处理当中遍历数组即可 -->
        Upload: <input type="file" name="upfile[]" value=""><br>
        <input type="file" name="upfile[]" value=""><br>
        <input type="file" name="upfile[]" value=""><br>
        <input type="submit" name="submit" value="提交">
    </form>
    </body>
</html>

<?php
/*
 Array
(
    [upfile] => Array
        (
            [name] => Array
                (
                    [0] => Penguin.jpg
                    [1] => Penguin.tif
                    [2] => Cat.jpeg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/tiff
                    [2] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => /Applications/MAMP/tmp/php/phprRPbJo
                    [1] => /Applications/MAMP/tmp/php/phpyvsayI
                    [2] => /Applications/MAMP/tmp/php/phpMCFyX6
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                )

            [size] => Array
                (
                    [0] => 199856
                    [1] => 229750
                    [2] => 206069
                )

        )

)
 */



if(isset($_POST['submit'])) {

    // 将临时目录下的上传的文件，复制到我指定的目录下，指定的名字就可以完成上传
    $num = count($_FILES['upfile']['name']);
    echo $num;
    for($i=0;$i<$num;$i++) {
        $error = $_FILES['upfile']['error'][$i];
        //第一步：检查错误
        if($error > 0) {
            switch ($error) {
                case '1':
                    echo "上传文件的大小超出了约定值";
                    break;
                case '2':
                    echo "上传文件大小超出了HTML表单隐藏域属性的MAX_FILE_SIZE元素所指定的最大值";
                    break;
                case '3':
                    echo "文件只被部分上传";
                    break;
                case '4':
                    echo "没有上传任何文件";
                    break;
                case '6':
                    echo "找不到临时文件夹";
                    break;
                case '7':
                    echo "文件写入失败";
                    break;

                default:
                    echo "not found error";
                    break;
            }
        }
        //第二步：判断类型
        if(($_FILES["upfile"]["type"][$i] == "image/gif")
            || ($_FILES["upfile"]["type"][$i] == "image/jpeg")
            || ($_FILES["upfile"]["type"][$i] == "image/png")
        ) {
                echo "文件类型正确";
        } else {
            echo "文件类型非法";
            exit;
        }

        // 第三步：判断大小
        $maxsize = 800000;
        if(!($_FILES["upfile"]["size"][$i] < $maxsize)){
            echo "文件超过了".$maxsize;
            exit;
        }

        // 第四步：随机文件名
        $name_arr = explode('.', basename($_FILES['upfile']['name'][$i])); //basename() 返回带扩展名的文件名
        $extendname = ".".array_pop($name_arr); //array_pop() 删除数组的最后一项，并返回删除的值

        $tmpfile = $_FILES['upfile']['tmp_name'][$i];
        // $srcname = "./upload/".$_FILES['upfile']['name'];
        $srcname = "./upload/".date(YmDHis).rand(100, 999).$extendname;

        if(move_uploaded_file($tmpfile, $srcname)) {
            echo $_FILES['upfile']['name'][$i]."Success!";
        } else {
            echo "Failed";
        }
    }
}
